
package ch.unisi.inf.gui;

import bejar.MyCellRenderder;
import ch.unisi.inf.gui.action.*;
import ch.unisi.inf.editor.ImageEditorFrame;
import ch.unisi.inf.engine.Image;
import ch.unisi.inf.viewer.MainImgViewerPanel;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;



/**
 * 
 * @author bejarj@lu.unisi.ch
 * 		   matthias.eberli@lu.unisi.ch 
 *
 */
public final class MainFrame extends JFrame {
	
	private final static int widthframe = 900; 
	private final static int heightframe = 600; 
	private final JButton openButton;
	private final JButton removeButton;
	private final JButton editButton;
	private final JButton printButton;
	private final JButton mailButton;
	private JPanel listPanel;
	private JPanel photoPanel;
	private JPanel buttonPanel;
	
	private JScrollPane scrollPhoto; 
	private JScrollPane scrollList;
	private JSplitPane split; 
	
	private Image selectedImage = null;
	
	private MainImgViewerPanel thumbPanel;
	
	private ArrayList<Image> holydayImages;
	private ArrayList<Image> dogImages;
	private ArrayList<Image> imageList;
	private ArrayList<Image> carImages;
	
	private final static int GAP_ICON = 0;
	//private JList list = new JList(data);
	private final JList list;
	
	public MainFrame(){
		dogImages = new ArrayList<Image>();
		dogImages.add(loadImage("src/images/dog1.jpg"));
		dogImages.add(loadImage("src/images/dog2.jpg"));
		dogImages.add(loadImage("src/images/dog3.jpg"));
		dogImages.add(loadImage("src/images/dog4.jpg"));
		
		holydayImages = new ArrayList<Image>();
		holydayImages.add(loadImage("src/images/beach.jpg"));
		holydayImages.add(loadImage("src/images/desktop.jpg")); 
		holydayImages.add(loadImage("src/images/beach2.jpg"));
		holydayImages.add(loadImage("src/images/maldive.jpg"));
		holydayImages.add(loadImage("src/images/Kanyon.jpg")); 
		holydayImages.add(loadImage("src/images/pacific.jpg"));
		holydayImages.add(loadImage("src/images/lake.jpg"));
		holydayImages.add(loadImage("src/images/mountain.jpg")); 
		holydayImages.add(loadImage("src/images/surf.jpg"));
		
		carImages = new ArrayList<Image>();
		carImages.add(loadImage("src/images/cars.jpg"));
		carImages.add(loadImage("src/images/cars2.jpg"));
		carImages.add(loadImage("src/images/car3.jpg"));
		
		imageList = new ArrayList<Image>();
		imageList.addAll(dogImages);
		imageList.addAll(holydayImages);
		imageList.addAll(carImages);
		
		this.setTitle("PhotoManager: version 01");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());
		this.setSize(widthframe, heightframe );
		this.setResizable(false);
		//this.pack();
		//Thread.sleep(5000);
		//this.setState(this.ICONIFIED);
		
		
		
		//>>>>>>>>>>>>>>>>>>>>>>>>>
		// BUTTONS
		//>>>>>>>>>>>>>>>>>>>>>>>>>
		
		openButton = new JButton(new OpenAction()); // here is the trick
		openButton.setText("");
		openButton.setIconTextGap(GAP_ICON);
		openButton.setIcon(new ImageIcon("src/images/open.png"));
		openButton.setRolloverIcon(new ImageIcon("src/images/openover.png"));
		openButton.setPressedIcon(new ImageIcon("src/images/openover.png"));
		openButton.setBorderPainted(false);
		openButton.setContentAreaFilled(false);
		
		
		
		removeButton = new JButton();//new RemoveAction());
		removeButton.setText("");
		removeButton.setIconTextGap(GAP_ICON);
		removeButton.setIcon(new ImageIcon("src/images/close.png"));
		removeButton.setRolloverIcon(new ImageIcon("src/images/closeover.png"));
		removeButton.setPressedIcon(new ImageIcon("src/images/closeover.png"));
		removeButton.setBorderPainted(false);
		removeButton.setContentAreaFilled(false);
		
		editButton = new JButton("Edit");
		editButton.setText("");
		editButton.setIconTextGap(GAP_ICON);
		editButton.setIcon(new ImageIcon("src/images/edit.png"));
		editButton.setRolloverIcon(new ImageIcon("src/images/editover.png"));
		editButton.setPressedIcon(new ImageIcon("src/images/editover.png"));
		editButton.setBorderPainted(false);
		editButton.setContentAreaFilled(false);
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev){
				ImageEditorFrame editor = new ImageEditorFrame(selectedImage);
			}
		});

		
		printButton = new JButton(new PrintAction());
		printButton.setText("");
		printButton.setIconTextGap(GAP_ICON);
		printButton.setIcon(new ImageIcon("src/images/print.png"));
		printButton.setRolloverIcon(new ImageIcon("src/images/printover.png"));
		printButton.setPressedIcon(new ImageIcon("src/images/printover.png"));
		printButton.setBorderPainted(false);
		printButton.setContentAreaFilled(false);
		
		thumbPanel = new MainImgViewerPanel(imageList, this);
		
		mailButton = new JButton("Mail");
		mailButton.setText("");
		mailButton.setIconTextGap(GAP_ICON);
		mailButton.setIcon(new ImageIcon("src/images/mail.png"));
		mailButton.setRolloverIcon(new ImageIcon("src/images/mailover.png"));
		mailButton.setPressedIcon(new ImageIcon("src/images/mailover.png"));
		mailButton.setBorderPainted(false);
		mailButton.setContentAreaFilled(false);
		mailButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev){
				MailGui mailGui = new MailGui(selectedImage);
			}
		});
		
		//>>>>>>>>>>>>>>>>>>>>>>>>
		//PANELS
		//>>>>>>>>>>>>>>>>>>>>>>>>
		
		buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(900, 100));
	
		buttonPanel.add(openButton);
		buttonPanel.add(removeButton);
		buttonPanel.add(editButton);
		buttonPanel.add(printButton);
		buttonPanel.add(mailButton);
		
		ArrayList<String> lista = new ArrayList<String>();
		lista.add("Library");
		lista.add("Vacations");
		lista.add("Animals");
		lista.add("Cars");
		
		
		Object[] arrayObject = lista.toArray();
		
		list = new JList(arrayObject);
		list.setCellRenderer(new MyCellRenderder());
		
		
		scrollPhoto = new JScrollPane(thumbPanel);// you have to work on it
		
		scrollList = new JScrollPane(list);// here u have to pass
		
		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollList, scrollPhoto);
		split.setOneTouchExpandable(true);
		split.setDividerLocation(200);
		
		
		MouseListener mouselistener = new ListChangeAction(this, holydayImages, dogImages,imageList, carImages, list);
			
		
		list.addMouseListener(mouselistener);
		
		final JMenuBar menuBar = new JMenuBar();
		
		final JMenu infoMenu = new JMenu("FM");
		infoMenu.setMnemonic('I');
		final JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		final JMenu editMenu = new JMenu("Edit");
		editMenu.setMnemonic('E');
		final JMenu windowMenu = new JMenu("Window");
		editMenu.setMnemonic('W');
		final JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('H');
		
		// items into fileMenu
		menuBar.add(infoMenu);
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(windowMenu);
		menuBar.add(helpMenu);
		
		//Actions
		
		final Action aboutAction = new AboutAction();
		final Action quitAction = new QuitAction(this);
		final Action printAction = new PrintAction();
		final Action openAction = new OpenAction();
		final Action mailAction = new MailAction(thumbPanel);
		final Action helpAction = new HelpAction();
		//final Action removeAction = new RemoveAction();
		final Action minimiseAction = new MinimiseAction(this);
		final Action editAction = new EditAction(this, selectedImage);
		
		//final Action printAction = new PrintAction(this);
		
		
		
		//Items holding the actions
		final JMenuItem about = new JMenuItem(aboutAction);
		final JMenuItem quit = new JMenuItem(quitAction);
		quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.META_MASK));
		
		final JMenuItem open = new JMenuItem(openAction);
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.META_MASK));
		final JMenuItem print = new JMenuItem(printAction);
		print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.META_MASK));
		final JMenuItem mail = new JMenuItem(mailAction);
		mail.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.META_MASK));
		
		final JMenuItem minimise = new JMenuItem(minimiseAction);
		minimise.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.META_MASK));
		
		final JMenuItem help = new JMenuItem(helpAction);
		help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, Event.META_MASK));
		
		// menus taken the item
		infoMenu.add(about);
		infoMenu.add(quit);
		fileMenu.add(open);
		fileMenu.add(print);
		fileMenu.add(mail);
		windowMenu.add(minimise);
		helpMenu.add(help);
		this.setJMenuBar(menuBar);
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(final WindowEvent ev){
				quitAction.actionPerformed(null);
				
			}
		});
		
		//System.setProperty("apple.laf.useScreenMenuBar", "true");

		
		//<--------------------------------------------------->
		// add components on the frame
		
		this.getContentPane().add(BorderLayout.CENTER, split);
		this.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		
		//<-----------------listener manager------------------>
		
		
		/**remove.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("remove");
			}
			
		});
		**/
		
	}
	//___________________________________________
	

	public void setSelectedImage(final Image image)
	{
		selectedImage = image;
	}

	
	 public static Image loadImage(String imagePath)
	 {
		 File file = new File(imagePath);
		 try {
			 BufferedImage image = ImageIO.read(file);
			 if(image == null || (image.getWidth(null) < 0)) {
				 return null;
			 }
			 return new Image(imagePath.toString(), image);
		 }
		 catch(IOException exc) {
			 return null;
		 }
	 }
	 
	 public void setThumbnailsFrame(final MainImgViewerPanel panel)
	 {
		 split.remove (scrollPhoto);
		 
		 thumbPanel = panel;
		 scrollPhoto = new JScrollPane(thumbPanel);// you have to work on it 
		 split.add(scrollPhoto);
		 split.setOneTouchExpandable(true);
		 split.setDividerLocation(200);
		 split.validate();
		 split.repaint();
		 
	 }
	
}
